{% load i18n %}
{% load bootstrap4 %}
{% load icons %}
<div role="tabpanel" class="tab-pane" id="settings">

    <!-- Nav tabs -->
    <ul class="nav nav-tabs" role="tablist" aria-label="Instance settings">
        {% if request.user.is_superuser %}
            {% if instance.guest_agent_ready %}
            <li class="nav-item">
                <a class="px-1 nav-link text-secondary" href="#osinfo" aria-controls="os-info" role="tab" data-toggle="tab" onclick="get_osinfo()">
                    <span class="fa fa-info-circle"></span>
                </a>
            </li>
            {% endif %}
            <li class="nav-item">
                <a class="nav-link text-secondary active" href="#boot_opt" aria-controls="boot" role="tab" data-toggle="tab">
                    {% trans "Boot" %}
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link text-secondary" href="#disks" aria-controls="disks" role="tab" data-toggle="tab">
                    {% trans "Disk" %}
                </a>
            </li>
        {% endif %}
        {% if request.user.is_superuser or request.user.is_staff or userinstance.is_vnc %}
            <li class="nav-item">
                <a class="nav-link text-secondary" href="#vncsettings" aria-controls="vncsettings" role="tab" data-toggle="tab">
                    {% trans "Console" %}
                </a>
            </li>
        {% endif %}
        {% if request.user.is_superuser %}
            <li class="nav-item">
                <a class="nav-link text-secondary" href="#network" aria-controls="network" role="tab" data-toggle="tab">
                    {% trans "Network" %}
                </a>
            </li>
        {% endif %}
        {% if perms.instances.clone_instances %}
            <li class="nav-item">
                <a class="nav-link text-secondary" href="#clone" aria-controls="clone" role="tab" data-toggle="tab">
                    {% trans "Clone" %}
                </a>
            </li>
        {% endif %}
        {% if request.user.is_superuser %}
            <li class="nav-item">
                <a class="nav-link text-secondary" href="#migrate" aria-controls="migrate" role="tab" data-toggle="tab">
                    {% trans "Migrate" %}
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link text-secondary" href="#xmledit" aria-controls="xmledit" role="tab" data-toggle="tab">
                    {% trans "XML" %}
                </a>
            </li>
        {% endif %}
        {% if perms.instances.clone_instances %}
            <li class="nav-item">
                <a class="nav-link text-secondary" href="#options" aria-controls="options" role="tab" data-toggle="tab">
                    {% trans "Options" %}
                </a>
            </li>
        {% endif %}
        {% if request.user.is_superuser %}
            <li class="nav-item">
                <a class="nav-link text-secondary" href="#users" aria-controls="users" role="tab" data-toggle="tab">
                    {% trans "Users" %}
                </a>
            </li>
        {% endif %}
    </ul>
    <!-- Tab panes -->
    <div class="tab-content">
        {% if request.user.is_superuser %}
            {% include 'instances/info_tab.html' %}
            <div role="tabpanel" class="tab-pane tab-pane-bordered active" id="boot_opt">
                <p class="font-weight-bold">{% trans 'Autostart' %}</p>
                    <div class="form-group row">
                        <div class="col-sm-12 text-center">
                        <p>{% trans "Autostart your instance when host server is power on " %}
                            {% if instance.autostart == 0 %}
                                <a class="btn btn-success" href="{% url 'instances:set_autostart' instance.id %}">{% trans "Enable" %}</a>
                            {% else %}
                                <a class="btn btn-danger" href="{% url 'instances:unset_autostart' instance.id %}">{% trans "Disable" %}</a>
                            {% endif %}
                        </p>
                        </div>
                    </div>
                <p class="font-weight-bold">{% trans 'Boot Order' %}</p>
                    <div class="form-group row">
                        <div class="col-sm-12 text-center">
                        {% if instance.status == 5 %}
                            <p>{% trans "Enable Boot Menu for your instance when it starts up " %}
                            {% if instance.bootmenu == 0 %}
                                <form action="{% url 'instances:set_bootmenu' instance.id %}" method="post" role="form" aria-label="Enable/disable instance boot order form">{% csrf_token %}
                                    <input type="submit" class="btn btn-success" name="set_bootmenu" title="{% trans 'Show boot menu' %}" value="{% trans "Enable" %}">
                                </form>
                            {% else %}
                                <form action="{% url 'instances:unset_bootmenu' instance.id %}" method="post" role="form" aria-label="Enable/disable instance boot order form">{% csrf_token %}
                                    <input type="submit" class="btn btn-danger" name="unset_bootmenu" title="{% trans 'Hide boot menu' %}" value="{% trans "Disable" %}">
                                </form>
                            {% endif %}
                        {% else %}
                            {% if instance.bootmenu == 0  %}
                                <p>**** {% trans "Please shutdown instance to modify boot menu" %} ****</p>
                            {% endif %}
                        {% endif %}
                        </div>
                    </div>

                {% if instance.bootmenu == 1  %}
                    <div class="d-flex justify-content-center">
                        <div class="col-sm-6 bg-light rounded shadow-sm">
                            {% for idx, val in instance.boot_order.items %}
                                <label>{{ idx|add:1 }}:{{ val.target }}, </label>
                            {% endfor %}
                        </div>
                    </div>
                    <form action="{% url 'instances:set_bootorder' instance.id %}" method="post" role="form" aria-label="Boot order edit form">{% csrf_token %}
                        <input id="bootorder" name="bootorder" hidden>
                        <div class="d-flex justify-content-center">
                            <div id="b_order" class="multipleselect border-0">
                                {% for disk in instance.disks %}
                                    <label><input type="checkbox" name="disk:{{ disk.dev }}" value="disk:{{ disk.dev }}" onclick="set_orderlist($('#bootorder'))" />{{ disk.dev }} - {{ disk.image }}</label>
                                {% endfor %}
                                {% for cd in instance.media %}
                                    <label><input type="checkbox" name="cdrom:{{ cd.dev }}" value="cdrom:{{ cd.dev }}" onclick="set_orderlist($('#bootorder'))"/>{{ cd.dev }} - {{ cd.image }}</label>
                                {% endfor %}
                                {% for net in instance.networks %}
                                    <label><input type="checkbox" name="network:{{ net.mac }}" value="network:{{ net.mac }}" onclick="set_orderlist($('#bootorder'))"/>NIC - {{ net.mac|slice:"9:" }}</label>
                                {% endfor %}
                            </div>
                            <div>
                                <div class="row mt-4">
                                    <a href="#" id="boot_order_up" class="btn btn-light shadow-sm"><span class="fa fa-arrow-up" title="{% trans 'up: move selected devices' %}"></span></a>
                                </div>
                                <div class="row mt-2">
                                    <a href="#" id="boot_order_down" class="btn btn-light shadow-sm"><span class="fa fa-arrow-down" title="{% trans 'down: move selected devices' %}"></span></a>
                                </div>
                            </div>
                        </div>
                        <div class="d-flex justify-content-center">
                            <div class="col-sm-6">
                                <input type="submit" class="btn btn-success btn-block" name="set_bootorder" value="{% trans "Apply" %}">
                            </div>
                        </div>
                    </form>
                {% endif %}
                <div class="clearfix"></div>
            </div>
            <div role="tabpanel" class="tab-pane tab-pane-bordered" id="disks">
            <form action="{% url 'instances:add_cdrom' instance.id %}" method="post" role="form" aria-label="Add CD-ROM form">{% csrf_token %}
                <p class="font-weight-bold">
                    {% trans "Instance Media" %}
                    <button class="btn btn-success float-right"
                            type="submit" type="button"
                            title="{% trans 'Add CD-ROM' %}"
                            {% if instance.status != 5 %} disabled {% endif %}>
                        <span class="fa fa-plus"></span>
                    </button>
                </p>
            </form>
            <div class="clearfix"></div>
            {% for cd in instance.media %}
                <div class="row mt-2">
                    <a class="col-sm-3 col-form-label"
                            name="details"
                            title="{% trans "Details" %}"
                            tabindex="0"
                            data-trigger="focus"
                            data-toggle="popover"
                            data-html="true"
                            data-content="<strong>{% trans 'Bus' %}:</strong> {{ cd.bus }} <br/>
                                <strong>{% trans 'Device' %}:</strong> {{ cd.dev }}">
                        {% trans "CD-ROM" %} {{ forloop.counter }}
                    </a>
                    <div class="col-sm-9">
                        {% if not cd.image %}
                            <form action="{% url 'instances:mount_iso' instance.id %}" method="post">
                                {% csrf_token %}
                                <div class="input-group">
                                    <select name="media" class="form-control">
                                    {% if instance.media_iso %}
                                        {% for iso in instance.media_iso %}
                                            <option value="{{ iso }}">{{ iso }}</option>
                                        {% endfor %}
                                    {% else %}
                                        <option value="none">{% trans "None" %}</option>
                                    {% endif %}
                                    </select>
                                    <div class="input-group-append">
                                    {% if instance.media_iso and allow_admin_or_not_template %}
                                        <button type="submit" class="btn btn-sm btn-success float-left" name="mount_iso" value="{{ cd.dev }}">{% trans "Mount" %}</button>
                                    {% else %}
                                        <button class="btn btn-sm btn-success float-left disabled">{% trans "Mount" %}</button>
                                    {% endif %}
                                    {% if instance.status == 5 and allow_admin_or_not_template %}
                                        <a  href="{% url 'instances:detach_cdrom' instance.id cd.dev %}" class="btn btn-sm btn-danger float-right" title="{% trans "Detach CD-ROM (remove device)" %}">
                                            {% icon 'remove' %}
                                        </a>
                                    {% endif %}
                                    </div>
                                </div>
                            </form>
                        {% else %}
                            <form action="{% url 'instances:unmount_iso' instance.id %}" method="post">
                                {% csrf_token %}
                                <div class="input-group">
                                    <input type="text" class="form-control" value="{{ cd.image }}" disabled/>
                                    <div class="input-group-append">
                                        <input type="hidden" name="path" value="{{ cd.path }}">
                                        {% if allow_admin_or_not_template %}
                                            <button type="submit" class="btn btn-sm btn-success float-left" value="{{ cd.dev }}" name="umount_iso">{% trans "Unmount" %}</button>
                                        {% else %}
                                            <button class="btn btn-sm btn-success float-left disabled" value="{{ cd.dev }}" name="umount_iso">{% trans "Unmount" %}</button>
                                        {% endif %}
                                    </div>
                                </div>
                            </form>
                        {% endif %}
                    </div>
                </div>
            {% empty %}
                <div class="offset-3 col-sm-6">
                    <div class="bg-light rounded shadow-sm">{% trans 'There is not any CD-ROM device.' %}</div>
                </div>
            {% endfor %}
            <div class="clearfix"></div>
            <p class="font-weight-bold">
                {% trans "Instance Volume" %}
                {% include 'add_instance_volume.html' %}
            </p>

            <div class="col-12 col-sm-12">
                <div class="table-responsive">
                    <table class="table table-hover mt-3">
                        <thead>
                            <tr>
                                <th scope="col">{% trans "Device" %}</th>
                                <th scope="col">{% trans "Used" %}</th>
                                <th scope="col">{% trans "Capacity" %}</th>
                                <th scope="col">{% trans "Storage" %}</th>
                                <th scope="col">{% trans "Source" %}</th>
                                <th scope="col">{% trans "Action" %}</th>
                            </tr>
                        </thead>
                        <tbody>
                        {% for disk in instance.disks %}
                                <tr>
                                    <td>
                                        <button type="submit" class="btn btn-sm btn-secondary"
                                                name="details{{ forloop.counter0 }}"
                                                title="{% trans "Details" %}"
                                                tabindex="0"
                                                data-trigger="focus"
                                                data-toggle="popover"
                                                data-html="true"
                                                data-content="<strong>Bus:</strong> {{ disk.bus }} <br/>
                                                                <strong>Format:</strong> {{ disk.format }} <br/>
                                                                <strong>Cache:</strong> {{ disk.cache }} <br/>
                                                                <strong>Serial:</strong> {{ disk.serial }} <br/>
                                                                <strong>Readonly:</strong> {{ disk.readonly }} <br/>
                                                                <strong>Shareable:</strong> {{ disk.shareable }}</br>
                                                                <strong>IO Mode:</strong> {{ disk.io }} <br/>
                                                                <strong>Discard:</strong> {{ disk.discard }} <br/>
                                                                <strong>Detect Zeroes:</strong> {{ disk.detect_zeroes }}">
                                            <span class="fa fa-info"></span>
                                        </button>
                                        {{ disk.dev }}
                                    </td>
                                {% if disk.storage is None %}
                                    <td colspan="4">
                                        <div class="alert alert-danger">
                                            {% trans "Error getting disk info" %}
                                        </div>
                                    </td>
                                {% else %}
                                    <td>{{ disk.used | filesizeformat}}</td>
                                    <td>{{ disk.size | filesizeformat }}</td>
                                    <td>{{ disk.storage }}</td>
                                    <td>{{ disk.path }}</td>
                                {% endif %}
                                    <td class="text-nowrap">
                                        <form class="d-inline" action="{% url 'instances:edit_volume' instance.id %}" method="post" role="form" aria-label="Edit instance volume form">
                                            {% csrf_token %}
                                            <input type="hidden" name="path" value="{{ disk.path }}">
                                            <input type="hidden" name="dev" value="{{ disk.dev }}">
                                            <input type="hidden" name="storage" value="{{ disk.storage }}">
                                            <input type="hidden" name="name" value="{{ disk.image }}">
                                            {% include 'edit_instance_volume.html' with id=forloop.counter0 %}
                                        </form>
                                        <form class="d-inline" action="{% url 'instances:detach_vol' instance.id %}" method="post">
                                            {% csrf_token %}
                                            <input type="hidden" name="path" value="{{ disk.path }}">
                                            <input type="hidden" name="dev" value="{{ disk.dev }}">
                                            <input type="hidden" name="storage" value="{{ disk.storage }}">
                                            <input type="hidden" name="name" value="{{ disk.image }}">
                                            {% if instance.status == 5 %}
                                                <button type="submit" class="btn btn-sm btn-secondary" value="{{ disk.dev }}" title="{% trans "Detach" %}" onclick="return confirm('{% trans "Are you sure to detach volume?" %}')">
                                                    {% icon 'eject' %}
                                                </button>
                                            {% else %}
                                                <button class="btn btn-sm btn-secondary disabled" value="{{ disk.dev }}" title="{% trans "Detach" %}" onclick="return confirm('{% trans "Are you sure? This may lead data corruption!" %}')">
                                                    {% icon 'eject' %}
                                                </button>
                                            {% endif %}
                                        </form>
                                        <form class="d-inline" action="{% url 'instances:delete_vol' instance.id %}" method="post">
                                            {% csrf_token %}
                                            <input type="hidden" name="path" value="{{ disk.path }}">
                                            <input type="hidden" name="dev" value="{{ disk.dev }}">
                                            <input type="hidden" name="storage" value="{{ disk.storage }}">
                                            <input type="hidden" name="name" value="{{ disk.image }}">
                                            {% if instance.status == 5 %}
                                                <button type="submit" class="btn btn-sm btn-secondary" title="{% trans "Delete" %}" onclick="return confirm('{% trans "Are you sure to delete volume?" %}')">
                                                    <i class="fa fa-trash"></i>
                                                </button>
                                            {% else %}
                                                <button class="btn btn-sm btn-secondary disabled" title="{% trans "Delete" %}" onclick="return confirm('{% trans "Are you sure? This may lead data corruption!" %}')">
                                                    <i class="fa fa-trash"></i>
                                                </button>
                                            {% endif %}
                                        </form>
                                    </td>
                                </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        <div class="clearfix"></div>
    </div>
    <div role="tabpanel" class="tab-pane tab-pane-bordered" id="network">
        <p>
            {% trans "Add a network device" %}
            {% include 'add_instance_network_block.html' %}
        </p>

        <div class="row mt-3">
            <div class="col-lg-12 mt-3">
            <h5 class="font-weight-bold">{% trans "Network Devices" %}</h5>
            <table class="table">
                <thead>
                <tr>
                    <th scope="col">{% trans 'Name' %}</th>
                    <th scope="col" class="d-none d-table-cell d-sm-table-cell" colspan="6">{% trans 'Info' %}</th>
                    <th scope="colgroup" class="d-none" colspan="2">{% trans 'Info' %}</th>
                    <th scope="colgroup" colspan="2">{% trans 'Actions' %}</th>
                </tr>
                </thead>
                <tbody>
                {% for network in instance.networks %}
                    <tr>
                        <td rowspan="2">eth{{ forloop.counter0 }}({{ network.target|default:"no target" }})
                            <form action="{% url 'instances:set_link_state' instance.id %}" method="post" aria-label="set instance link state form">
                                {% csrf_token %}
                                <input name="mac" value="{{ network.mac }}" hidden/>
                                <input name="set_link_state" value="{{ network.state }}" hidden/>
                                <input type="checkbox" {% if network.state == 'up' %} checked{% endif %} onclick='submit();' />
                                <strong>{% trans 'active' %}</strong>
                                <small>{{ network.type }}</small>
                            </form>
                        </td>
                        <th class="d-none d-table-cell d-sm-table-cell">{% trans 'MAC' %}</th>
                        <td>{{ network.mac }}</td>
                        <th scope="row" class="d-none d-table-cell d-sm-table-cell">{% trans 'Filter' %}</th>
                        <td class="d-none d-table-cell">{{ network.filterref|default:"None" }}</td>
                        <th scope="row" class="d-none d-table-cell d-sm-table-cell">{% trans 'Source' %}</th>
                        <td>{{ network.nic }}</td>
                        <td>
                        <form action="{% url 'instances:change_network' instance.id %}" method="post" name="edit_network{{ forloop.counter0 }}" role="form">{% csrf_token %}
                            <button data-target="#editInstanceNetwork{{ forloop.counter0 }}" type="button" class="btn btn-sm btn-primary"
                                    title="{% trans "Edit NIC" %}" data-toggle="modal">
                                    <span class="fa fa-edit" aria-hidden="true"></span>
                            </button>

                            <div class="modal fade" id="editInstanceNetwork{{ forloop.counter0 }}" role="dialog" aria-labelledby="editInstanceNetworkLabel" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title">{% trans "Edit Instance Network" %}</h5>
                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                        </div>
                                        <div class="modal-body">
                                            <div class="container">
                                            <div class="form-group row">
                                                <label class="col-form-label">{% trans "MAC" %}</label>
                                                <div class="input-group">
                                                    <input class="form-control" type="text" value="{{ network.mac }}" readonly/>
                                                    <input class="form-control" type="text" name="net-mac-{{ forloop.counter0 }}" value="{{ network.mac }}"/>
                                                </div>
                                            </div>
                                            <div class="form-group row">
                                                <label class="col-form-label">{% trans "Net Source" %}</label>
                                                <div class="input-group">
                                                    <input class="form-control" type="text" value="{{ network.nic }}" readonly/>
                                                    <select class="form-control" name="net-source-{{ forloop.counter0 }}">
                                                        {% for c_net in networks_host %}
                                                        <option value="net:{{ c_net }}" {% if c_net == network.nic %} selected {% endif %}>{% trans 'Network' %} {{ c_net }}</option>
                                                        {% endfor %}
                                                        {% for c_iface in instance.interfaces %}
                                                        <option value="iface:{{ c_iface }}" {% if c_iface == network.nic %} selected {% endif %}>{% trans 'Interface' %} {{ c_iface }}</option>
                                                        {% endfor %}
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="form-group row">
                                                <label class="col-form-label">{% trans "NWFilter" %}</label>
                                                <div class="input-group">
                                                    <input class="form-control" type="text" value="{{ network.filterref }}" readonly/>
                                                    <select class="form-control" name="net-nwfilter-{{ forloop.counter0 }}">
                                                        <option value="">{% trans "None" %}</option>
                                                        {% for c_filters in nwfilters_host %}
                                                        <option value="{{ c_filters }}" {% if c_filters == network.filterref  %} selected {% endif %}>{{ c_filters }}</option>
                                                        {% endfor %}
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="form-group row">
                                                <label class="col-form-label">{% trans "Model" %} </label>
                                                <div class="input-group">
                                                    <input class="form-control" type="text" value="{{ network.model }}" readonly/>
                                                    <select class="form-control" name="net-model-{{ forloop.counter0 }}">
                                                        {% for model in net_models_host %}
                                                        <option value="{{ model }}" {% if model == network.model  %} selected {% endif %}>{{ model }}</option>
                                                        {% endfor %}
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                        </div>
                                        <div class="modal-footer">
                                            <button class="btn btn-secondary" data-dismiss="modal">{% trans 'Close' %}</button>
                                            <button class="btn btn-success" name="change_network" title="{% trans "Apply network changes" %}">{% trans "Apply" %}</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                        </td>
                        <td align="right">
                            <form action="{% url 'instances:delete_network' instance.id %}" method="post" name="delete_network" role="form">{% csrf_token %}
                                <button class="btn btn-sm btn-danger" value="{{ network.mac }}" name="delete_network" title="{% trans "Delete Device" %}"
                                    onclick="return confirm('{% trans "Are you sure?" %}')">
                                    <i class="fa fa-trash"></i>
                                </button>
                            </form>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">{% trans 'IPv4' %}</th>
                        <td>
                            {% for ipv4 in network.ipv4|default:"unknown" %}{{ ipv4 }}{% endfor %}
                        </td>
                        <th scope="row">{% trans 'IPv6' %}</th>
                        <td>
                            {% for ipv6 in network.ipv6|default:"unknown" %}{{ ipv6 }}{% endfor %}
                        </td>
                        <th scope="row">{% trans 'Model' %}</th>
                        <td>{{ network.model }}</td>
                        <th>{% trans 'QoS' %}</th>
                        <td class="d-flex justify-content-end">
                            <form action="{% url 'instances:set_qos' instance.id %}" method="post" name="add_qos{{ forloop.counter0 }}" role="form" aria-label="Add network qos form">
                                {% csrf_token %}
                                <input type="text" name="net-mac-{{ forloop.counter0 }}" value="{{ network.mac }}" hidden/>
                                {% include 'add_network_qos.html' with id=forloop.counter0 %}
                            </form>
                        </td>
                    </tr>
                    <tr>
                        <td class="py-1 bg-primary text-white" colspan="9">
                            {% if network.type == 'direct' %}
                            <small>{% trans 'In most configurations, macvtap does not work for host to guest network communication' %}</small>
                            {% endif %}
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        </div>

        {% if instance.qos %}
        <div class="row mt-3">
            <div class="col-sm-10">
                <p><strong>{% trans "QoS Configuration" %}</strong></p>
            </div>
            <div class="col-12 col-sm-12">
                <table class="table table-hover">
                    <thead>
                    <tr class="d-flex">
                        <th scope="col" class="col-2">{% trans "MAC" %}/{% trans "Direction" %}</th>
                        <th scope="col" class="col-2">{% trans "Average" %}</th>
                        <th scope="col" class="col-3">{% trans "Peak" %}</th>
                        <th scope="col" class="col-3">{% trans "Burst" %}</th>
                        <th scope="col" class="col-2">{% trans "Actions" %}</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for q, attrs in instance.qos.items %}
                        {% for att in attrs  %}
                            <tr class="d-flex">
                                <form action="{% url 'instances:set_qos' instance.id %}" method="post" role="form" aria-label="Instance QoS configuration form">
                                    {% csrf_token %}
                                    <td class="col-2"><label class="col-form-label">{{ q }} {{ att.direction | capfirst }}</label></td>
                                    <td class="col-2"><input id="qos_average" class="form-control" name="qos_average"
                                            value="{{ att.average|default:'' }}"/>
                                    </td>
                                    <td class="col-3"><input id="qos_peak" class="form-control" name="qos_peak"
                                            value="{{ att.peak|default:'' }}"/>
                                    </td>
                                    <td class="col-3"><input id="qos_burst" class="form-control" name="qos_burst"
                                            value="{{ att.burst|default:'' }}"/>
                                    </td>
                                    <td class="col-sm-2">
                                        <input name="qos_direction" value="{{ att.direction }}" hidden/>
                                        <input name="net-mac" value="{{ q }}" hidden/>
                                        <button type="submit" class="btn btn-sm btn-primary"
                                                name="set_qos" data-toggle="modal"
                                                title="{% trans "Edit QoS" %}" onclick="return confirm('{% trans "Are you sure?" %}')">
                                            <span class="fa fa-save"></span>
                                        </button>
                                </form>
                                        <form action="{% url 'instances:unset_qos' instance.id %}" method="post" role="form" aria-label="Instance QoS configuration form">
                                            {% csrf_token %}
                                            <input name="qos_direction" value="{{ att.direction }}" hidden/>
                                            <input name="net-mac" value="{{ q }}" hidden/>
                                            <button type="submit" class="btn btn-sm btn-danger"
                                                    name="unset_qos"
                                                    title="{% trans "Delete QoS" %}" onclick="return confirm('{% trans "Are you sure?" %}')">
                                                <i class="fa fa-trash"></i>
                                            </button>
                                        </form>
                                    </td>
                            </tr>
                        {% endfor %}
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
        {% endif %}
        <div class="clearfix"></div>
    </div>
    <div role="tabpanel" class="tab-pane tab-pane-bordered" id="migrate">
        <p>{% trans "For migration both host servers must have equal settings and OS type" %}</p>
        <form action="{% url 'instances:migrate' instance.id %}" class="ml-3 form" method="post" role="form" aria-label="Migrate instance form">
            {% csrf_token %}
            <div class="form-group row">
                <label class="col-sm-3 col-form-label">{% trans "Original host" %}</label>
                <div class="col-sm-6">
                    <label class="form-control" readonly="readonly">{{ compute.name }}</label>
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-3 col-form-label">{% trans "Host migration" %}</label>
                <div class="col-sm-6">
                    <select name="compute_id" class="custom-select">
                        {% if computes_count != 1 %}
                            {% for comp in computes %}
                                {% if comp.id != compute.id %}
                                    <option value="{{ comp.id }}">{{ comp.name }}</option>
                                {% endif %}
                            {% endfor %}
                        {% endif %}
                    </select>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-sm-6 offset-3">
                    <div class="custom-control custom-switch">
                        <input class="custom-control-input" type="checkbox" name="live_migrate" value="true" id="vm_live_migrate" {% if instance.status != 5 %}checked{% else %}disabled{% endif %}>
                        <label class="custom-control-label" for="vm_live_migrate">{% trans "Live migration" %}</label>
                    </div>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-sm-6 offset-3">
                    <div class="custom-control custom-switch">
                        <input class="custom-control-input" type="checkbox" name="unsafe_migrate" value="true" id="vm_unsafe_migrate">
                        <label class="custom-control-label" for="vm_unsafe_migrate">{% trans "Unsafe migration" %}</label>
                    </div>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-sm-6 offset-3">
                    <div class="custom-control custom-switch">
                        <input class="custom-control-input" type="checkbox" name="xml_delete" value="true" id="xml_delete" checked>
                        <label class="custom-control-label" for="xml_delete">{% trans "Delete original" %}</label>
                    </div>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-sm-6 offset-3">
                    <div class="custom-control custom-switch">
                        <input class="custom-control-input" type="checkbox" name="offline_migrate" value="true" id="offline_migrate" {% if instance.status == 5 %}checked{% else %}disabled{% endif %}>
                        <label class="custom-control-label" for="offline_migrate">{% trans "Offline migration" %}</label>
                    </div>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-sm-6 offset-3">
                    <div class="custom-control custom-switch">
                        <input class="custom-control-input" type="checkbox" name="postcopy" value="true" id="postcopy" {% if instance.status != 1 %}disabled{% endif %}>
                        <label class="custom-control-label" for="postcopy">{% trans "Post copy" %}</label>
                    </div>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-sm-6 offset-3">
                    <div class="custom-control custom-switch">
                        <input class="custom-control-input" type="checkbox" name="autoconverge" value="true" id="autoconverge" {% if instance.status != 1 %}disabled{% endif %}>
                        <label class="custom-control-label" for="autoconverge" title="{% trans 'Forces CPU convergence during live migration' %}">{% trans "Auto converge" %}</label>
                    </div>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-sm-6 offset-3">
                    <div class="custom-control custom-switch">
                        <input class="custom-control-input" type="checkbox" name="compress" value="true" id="compress" {% if instance.status != 1 %}disabled{% endif %}>
                        <label class="custom-control-label" for="compress" title="{% trans 'Compress instance memory for fast migration' %}">{% trans "Compressed" %}</label>
                    </div>
                </div>
            </div>
            {% if computes_count != 1 %}
                <button type="submit" class="btn btn-lg btn-success float-right" name="migrate" onclick="showPleaseWaitDialog();">{% trans "Migrate" %}</button>
            {% else %}
                <button class="btn btn-lg btn-success float-right disabled">{% trans "Migrate" %}</button>
            {% endif %}
        </form>
        <div class="clearfix"></div>
    </div>
    <div role="tabpanel" class="tab-pane tab-pane-bordered" id="xmledit">
        <p>{% trans "If you need to edit XML please Power Off the instance" %}</p>
        <form action="{% url 'instances:change_xml' instance.id %}" method="post" role="form" aria-label="Edit instance XML form">
            {% csrf_token %}
            <div class="col-sm-12" id="xmlheight">
                <textarea id="editor">{{ instance.inst_xml }}</textarea>
            </div>
            {% if instance.status == 5 %}
                <input type="hidden" name="inst_xml">
                <button type="submit" class="btn btn-lg btn-success float-right" name="change_xml">
                    {% trans "Change" %}
                </button>
            {% else %}
                <button class="btn btn-lg btn-success float-right disabled">
                    {% trans "Change" %}
                </button>
            {% endif %}
        </form>
        <div class="clearfix"></div>
    </div>
    <div role="tabpanel" class="tab-pane tab-pane-bordered" id="users">
        <div>
        <p class="font-weight-bold">
            {% trans "Instance owners" %}
            {% include 'add_instance_owner_block.html' %}
        </p>
        </div>
        <div class="table-responsive">
            <table class="table table-striped sortable-theme-bootstrap mt-3" data-sortable>
                <tbody class="searchable">
                {% for userinstance in userinstances %}
                    <tr>
                        <td><a href="{% url 'accounts:account' userinstance.user.id %}">{{ userinstance.user }}</a></td>
                        <td style="width:30px;">
                            <a href="{% url 'accounts:user_instance_delete' userinstance.id %}?next={% url 'instances:instance' instance.id %}#users">
                                {% icon 'trash' %}
                            </a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        <div class="clearfix"></div>
    </div>
{% endif %}
{% if request.user.is_superuser or request.user.is_staff or userinstance.is_vnc %}
    <div role="tabpanel" class="tab-pane tab-pane-bordered" id="vncsettings">
        <form method="post" action="{% url 'instances:update_console' instance.id %}">
            {% csrf_token %}
            {% if instance.status != 5 %}
                <div class="alert alert-warning">
                    {% trans "To change console settings, shutdown the instance." %}
                </div>
            {% endif %}
            {% bootstrap_form console_form layout='horizontal' %}
            <div class="float-right">
                {% if instance.status != 5 %}
                    <button class="btn btn-success" name="set_console_type" disabled>{% trans "Update" %}</button>
                {% else %}
                    <button type="submit" class="btn btn-success ">{% trans "Update" %}</button>
                {% endif %}
            </div>
        </form>
        <div class="clearfix"></div>
    </div>
{% endif %}
    {% if perms.instances.clone_instances %}
        <div role="tabpanel" class="tab-pane tab-pane-bordered" id="clone">
            <p class="font-weight-bold">{% trans "Create a clone" %}</p>
            <form class="form" action="{% url 'instances:clone' instance.id %}" method="post" role="form" aria-label="Create clone form">{% csrf_token %}
                <div class="form-group row">
                    <label class="col-sm-3 col-form-label font-weight-normal">{% trans "Clone Name" %}</label>
                    {% if request.user.is_superuser %}
                        <div class="col-sm-6">
                        <div class="input-group">
                                <input id="clone_name" type="text" class="form-control" name="name" value="{{ instance.name }}-clone"/>
                                <div class="input-group-append">
                                    <button type="button" class="btn btn-success" name="guess-clone-name"
                                            onclick="guess_clone_name()">{% trans "Guess" %}</button>
                                </div>
                            </div>
                        </div>
                    {% elif app_settings.CLONE_INSTANCE_AUTO_NAME == 'True'%}
                        <div class="col-sm-4">
                            <input id="clone_instance_auto_name" type="text" class="form-control" name="auto_name" value="Automatic" disabled/>
                        </div>
                    {% else %}
                        <div class="col-sm-4">
                            <select id="select_clone_name" class="form-control" name="name" size="1">
                            {% for name in clone_free_names %}
                                <option value="{{ name }}">{{ name }}</option>
                            {% endfor %}
                            </select>
                        </div>
                    {% endif %}
                </div>
                {% if request.user.is_superuser %}
                    <label>{% trans "Network devices" %}:</label>
                    {% for network in instance.networks %}
                    <p>
                        <div class="form-group row">
                            <label class="col-sm-2 col-form-label offset-1">eth{{ forloop.counter0 }} ({{ network.nic }})</label>
                            <div class="col-sm-6">
                            <div class="input-group">
                                    <input type="text" class="form-control" name="clone-net-mac-{{ forloop.counter0 }}" value="{{ network.mac }}"/>
                                    <div class="input-group-append">
                                        <button type="button" class="btn btn-success" name="random-mac-{{ forloop.counter0 }}"
                                                onclick="random_mac('clone-net-mac-{{ forloop.counter0 }}')">{% trans "Random" %}</button>
                                        <button type="button" class="btn btn-success" name="guess-mac-{{ forloop.counter0 }}"
                                                onclick="guess_mac_address('#clone_name', {{ forloop.counter0 }})">{% trans "Guess" %}</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </p>
                    {% endfor %}
                {% else %}
                    {% for network in instance.networks %}
                        <input type="hidden" class="form-control" name="clone-net-mac-{{ forloop.counter0 }}" value="{{ network.mac }}"/>
                    {% endfor %}
                {% endif %}
                {% if request.user.is_superuser %}
                    <label>{% trans "Storage devices" %}:</label>
                    {% for disk in instance.disks %}
                        <div class="form-group row">
                            <label class="col-sm-2 col-form-label offset-1">{{ disk.dev }} ({{ disk.storage }})</label>
                            <div class="col-sm-6">
                            <div class="input-group">
                                    <input id="disk_name-{{ disk.dev }}" type="text" class="form-control" name="disk-{{ disk.dev }}" value="{{ disk.image }}"/>
                                    {% if disk.format == 'qcow2' %}
                                        <div class="input-group-append">
                                            <span class="input-group-text" >{% trans 'Metadata' %}</span>
                                            <div class="input-group-text">
                                                <input type="checkbox" name="meta-{{ disk.dev }}" value="true">
                                            </div>
                                        </div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    {% endfor %}
                {% else %}
                    {% for disk in instance.disks %}
                        <input id="disk_name-{{ disk.dev }}" type="hidden" class="form-control" name="disk-{{ disk.dev }}" value="{{ disk.image }}"/>
                    {% endfor %}
                {% endif %}
                <div class="form-group row">
                    <label class="col-sm-3 col-form-label">{% trans "Title" %}</label>
                    <div class="col-sm-6">
                        <input type="text" name="clone-title" class="form-control">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-3 col-form-label">{% trans "Description" %}</label>
                    <div class="col-sm-6">
                        <textarea name="clone-description" class="form-control"></textarea>
                    </div>
                </div>
                {% if instance.status == 5 %}
                    <button type="submit" class="btn btn-lg btn-success float-right" name="clone" onclick="showPleaseWaitDialog();">{% trans "Clone" %}</button>
                {% else %}
                    <button class="btn btn-lg btn-success float-right disabled" name="clone">{% trans "Clone" %}</button>
                {% endif %}
            </form>
            <div class="clearfix"></div>
        </div>
        <div role="tabpanel" class="tab-pane tab-pane-bordered" id="options">
            <p>{% trans "To set instance template name description, shutdown the instance." %}</p>
            <form action="{% url 'instances:change_options' instance.id %}" method="post" role="form" aria-label="Set instance template name description form">{% csrf_token %}
                <div class="form-group row">
                    <label class="col-sm-3 col-form-label">{% trans "Title" %}</label>
                    <div class="col-sm-6">
                        <input type="text" name="title" class="form-control" value="{{ instance.title }}">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-3 col-form-label">{% trans "Description" %}</label>
                    <div class="col-sm-6">
                        <textarea name="description" class="form-control">{{ instance.description }}</textarea>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-3 col-form-label">{% trans "Is template" %}</label>
                    <div class="col-sm-6">
                        <input type="checkbox"
                            name="is_template"
                            value="True"
                            id="is_template"
                            {% if instance.is_template %}checked{% endif %}
                            {% if not request.user.is_superuser and not request.user.is_staff %}disabled{% endif %}>
                    </div>
                </div>
                <div class="form-group row">
                    <div class="offset-3 col-sm-6">
                    {% if instance.status == 5 %}
                        <button type="submit" class="btn btn-block btn-success" name="change_options">{% trans "Change" %}</button>
                    {% else %}
                        <button class="btn btn-block btn-success disabled" name="change_options">{% trans "Change" %}</button>
                    {% endif %}
                    </div>
                </div>
            </form>
        
            <p class="font-weight-bold">{% trans "To set instance video model, shutdown the instance." %}</p>
            <form action="{% url 'instances:set_video_model' instance.id %}" method="post" role="form" aria-label="Set instance video model form">{% csrf_token %}
                <div class="form-group row">
                    <label for="video_model_select" class="col-sm-3 col-form-label">{% trans "Primary Video Model" %}</label>
                    <div class="col-sm-6">
                        <div class="input-group">
                        <select id="video_model_select" class="custom-select" name="video_model">
                            <option class="font-weight-bold" value="">{% trans "please choose" %}</option>
                            {% for vmodel in instance.video_models %}
                                <option value="{{ vmodel }}"{% if vmodel == instance.video_model %} selected{% endif %}>{{ vmodel }}</option>
                            {% endfor %}
                        </select>
                        <span class="input-group-btn">
                            {% if instance.status == 5 %}
                            <button type="submit" class="btn btn-success" name="set_video_model">{% trans "Set" %}</button>
                            {% else %}
                            <button class="btn btn-success disabled" name="set_video_model">{% trans "Set" %}</button>
                            {% endif %}
                        </span>
                        </div>
                    </div>
                </div>
            </form>
    
            <p class="font-weight-bold">{% trans "To set instance vCPUs hotpluggable" %}</p>
            <form  action="{% url 'instances:set_vcpu_hotplug' instance.id %}" method="post" role="form" aria-label="Set instance vCPUs hotpluggable form">{% csrf_token %}
                <div class="form-group row">
                    <label for="vcpu_hotplug" class="col-sm-3 col-form-label">{% trans "vCPU Hot Plug" %}</label>
                    <div class="col-sm-6">
                        <div class="input-group">
                        <select id="vcpu_hotplug" class="form-control" name="vcpu_hotplug">
                            <option value="True" {% if instance.vcpus %} selected {% endif %}>{% trans 'Enabled' %}</option>
                            <option value="False" {% if not instance.vcpus %} selected {% endif %}>{% trans 'Disabled' %}</option>
                        </select>
                        <span class="input-group-btn">
                        {% if instance.status == 5 %}
                            <button type="submit" class="btn btn-success" name="set_vcpu_hotplug">{% trans "Set" %}</button>
                        {% else %}
                            <button class="btn btn-success" name="set_vcpu_hotplug" disabled>{% trans "Set" %}</button>
                        {% endif %}
                        </span>
                        </div>
                    </div>
                </div>
            </form>
        
            <p class="font-weight-bold">{% trans "To Enable/Disable Qemu Guest Agent. Status" %}:
            {% if instance.status == 1 %}
                {% if instance.guest_agent_ready %}
                    <label class="badge badge-success">{% trans 'Connected' %}</label>
                {% else %}
                    <label class="badge badge-danger">{% trans 'Disconnected' %}</label>
                {% endif %}</p>
            {% else %}
                <label class="badge badge-default">{% trans 'Unknown' %}</label>
            {% endif %}
            <form action="{% url 'instances:set_guest_agent' instance.id %}" method="post" role="form" aria-label="Enable/Disable Qemu Guest Agent form">{% csrf_token %}
                <div class="form-group row">
                    <label for="guest_agent" class="col-sm-3 col-form-label">{% trans "Qemu Guest Agent" %}</label>
                    <div class="col-sm-6">
                        <div class="input-group">
                        <select id="guest_agent" class="custom-select" name="guest_agent">
                            <option value="True" {% if instance.guest_agent %} selected {% endif %}>{% trans 'Enabled' %}</option>
                            <option value="False" {% if not instance.guest_agent %} selected {% endif %}>{% trans 'Disabled' %}</option>
                        </select>
                        <span class="input-group-btn">
                            <button type="submit" class="btn btn-success" name="set_guest_agent">{% trans "Set" %}</button>
                        </span>
                        </div>
                    </div>
                </div>
            </form>
            <div class="clearfix"></div>
        </div>
    {% endif %}
    </div>
</div>
